home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 4 / Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso / Development / Source / Chess++ ƒ / CChessPieces ƒ / CPawn.cp < prev    next >
Text File  |  1993-05-26  |  3KB  |  121 lines

  1. ////////////
  2. //
  3. //    CPawn.cp
  4. //
  5. //    Chess Piece methods for implementing a Pawn.
  6. //
  7. //    Copyright © 1993 Steven J. Bushell. All rights reserved.
  8. //
  9. ////////////
  10.  
  11. #include <stdlib.h>
  12.  
  13. #include "CChessBoard.h"
  14. #include "CChessPiece.h"
  15. #include "CPawn.h"
  16.  
  17. extern        CIconHandle    gWhitePawnCicnHandle;
  18. extern        CIconHandle    gBlackPawnCicnHandle;
  19.  
  20. void CPawn::IPawn(Boolean aColor)
  21. {
  22.     inherited::IChessPiece(aColor);
  23.     itsValue = 0x0100;
  24. }
  25.  
  26. void CPawn::Draw(short rank, short file)
  27. {
  28.     CIconHandle cicnHandle;
  29.     Rect aRect;
  30.     short rankQD = (rank - 1) << 5, fileQD = (file - 1) << 5;
  31.     
  32.     if (itsColor == White)
  33.         cicnHandle = gWhitePawnCicnHandle;
  34.     else
  35.         cicnHandle = gBlackPawnCicnHandle;
  36.  
  37.     aRect.left = rankQD;
  38.     aRect.right = rankQD+32;
  39.     aRect.top = fileQD;
  40.     aRect.bottom = fileQD+32;
  41.     PlotCIcon(&aRect,cicnHandle);
  42. }
  43.  
  44. CIconHandle    CPawn::GetCicnHandle(void)
  45. {
  46.     return gWhitePawnCicnHandle;
  47. }
  48.  
  49.  
  50. Boolean    CPawn::IsValidMove(CChessBoard *aBoard, short newRank, short newFile)
  51. {
  52.     short    oldRank = aBoard->firstClickRank, oldFile = aBoard->firstClickFile,
  53.             newColor,
  54.             myColor = aBoard->theBoard[oldRank][oldFile]->itsColor,
  55.             theOtherPlayersColor;
  56.  
  57.     if (myColor == White)
  58.         theOtherPlayersColor=Black;
  59.     else
  60.         theOtherPlayersColor=White;
  61.  
  62.     if (aBoard->theBoard[newRank][newFile])
  63.         newColor = aBoard->theBoard[newRank][newFile]->itsColor;
  64.     else
  65.         newColor = NoColor;
  66.     
  67.     if ((oldRank == newRank) && (newColor == NoColor))
  68.     {
  69.         if (((oldFile - newFile) == 1) && (myColor == White))
  70.             return true;
  71.         if (((newFile - oldFile) == 1) && (myColor == Black))
  72.             return true;
  73.         if (((oldFile - newFile) == 2) && (myColor == White) &&
  74.             (oldFile == 7) && (!(aBoard->theBoard[oldRank][6])))
  75.             return true;
  76.         if (((newFile - oldFile) == 2) && (myColor == Black) &&
  77.             (oldFile == 2) && (!(aBoard->theBoard[oldRank][3])))
  78.             return true;
  79.     }
  80.     if ((abs(oldRank-newRank) == 1) && (newColor == theOtherPlayersColor))
  81.     {
  82.         if (((oldFile - newFile) == 1) && (myColor == White))
  83.             return true;
  84.         if (((newFile - oldFile) == 1) && (myColor == Black))
  85.             return true;
  86.     }
  87.     return false;
  88. }
  89.  
  90.  
  91. short    CPawn::BoardLocationValue(CChessBoard *aBoard, short rank, short file)
  92. {
  93.     short killFile;
  94.     CChessPiece    *leftTarget,*rightTarget;
  95.     short    totalValue;
  96.  
  97.     totalValue = inherited::BoardLocationValue(aBoard,rank,file);
  98.  
  99.     killFile = file + ((itsColor == White) ? -1 : 1);
  100.     
  101.     leftTarget = aBoard->theBoard[rank-1][killFile];
  102.     rightTarget = aBoard->theBoard[rank+1][killFile];
  103.  
  104.     if (leftTarget)
  105.         if (itsColor == leftTarget->itsColor)
  106.             totalValue += 
  107.                 ((leftTarget->itsValue == kKingValue) ? 0 : leftTarget->itsValue) >> 5;
  108.     
  109.     if (rightTarget)
  110.         if (itsColor == rightTarget->itsColor)
  111.             totalValue +=
  112.                 ((rightTarget->itsValue == kKingValue) ? 0 : rightTarget->itsValue) >> 5;
  113.  
  114.     return totalValue;
  115. }
  116.  
  117.  
  118. void CPawn::RegisterMove(short rank, short file)
  119. {
  120.     inherited::RegisterMove(rank,file);
  121. }